//package com.example.commontest.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.github.pagehelper.PageInterceptor;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
//import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
//import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import tk.mybatis.spring.annotation.MapperScan;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
//@Configuration
//@MapperScan(basePackages = {"com.biapp.appboard.mapper.appboard","com.biapp.appboard.mapper.operation","com.biapp.appboard.mapper.installation"}, sqlSessionTemplateRef = "biAppSqlSessionTemplate")
//public class BiAppDataSourceConfiguration {
//
//    @Value("${spring.shardingsphere.props.sql.show}")
//    private String sqlShow;
//
//    @Bean
//    @ConfigurationProperties(prefix = "spring.shardingsphere.datasource.bi-master")
//    public DataSource biAppMasterDataSource() {
//        return new DruidDataSource();
//    }
//
//    @Bean
//    @ConfigurationProperties(prefix = "spring.shardingsphere.datasource.bi-slave")
//    public DataSource biAppSlaveDataSource() {
//        return new DruidDataSource();
//    }
//
//    @Bean
//    public DataSource shardingDataSource(@Qualifier("biAppMasterDataSource")DataSource biAppMasterDataSource,
//                                            @Qualifier("biAppSlaveDataSource")DataSource biAppSlaveDataSource) throws SQLException {
//        // Configure actual data sources
//        Map<String, DataSource> dataSourceMap = new HashMap<>();
//
//        // Configure master data source
//        dataSourceMap.put("bi-master", biAppMasterDataSource);
//
//        // Configure the first slave data source
//        dataSourceMap.put("bi-slave", biAppSlaveDataSource);
//
//        // Configure read-write split rule
//        LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration = new LoadBalanceStrategyConfiguration("round_robin");
//        MasterSlaveRuleConfiguration masterSlaveRuleConfig =
//                new MasterSlaveRuleConfiguration("ds_master_slave", "bi-master", Arrays.asList("bi-slave"), loadBalanceStrategyConfiguration);
//
//        // Get data source
//        Properties properties = new Properties();
//        properties.setProperty("sql.show", sqlShow);
//
//        return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, properties);
//    }
//
//    @Bean
//    public Interceptor pageHelper () {
//        Interceptor pageHelper = new PageInterceptor();
//        Properties properties = new Properties();
//        properties.setProperty("reasonable", "false");
//        properties.setProperty("helperDialect", "mysql");
//        pageHelper.setProperties(properties);
//        return pageHelper;
//    }
//
//    @Primary
//    @Bean
//    public SqlSessionFactory biAppSqlSessionFactory(
//            @Qualifier("shardingDataSource")DataSource shardingDataSource, Interceptor pageHelper) {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(shardingDataSource);
//        //添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        Interceptor[] plugins =  new Interceptor[]{pageHelper};
//        bean.setPlugins(plugins);
//        try {
//            bean.setMapperLocations(resolver.getResources("classpath:com/biapp/appboard/mapper/appboard/*/*.xml"));
//            return bean.getObject();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Primary
//    @Bean
//    public SqlSessionTemplate biAppSqlSessionTemplate(@Qualifier("biAppSqlSessionFactory")SqlSessionFactory biAppSqlSessionFactory) {
//        return new SqlSessionTemplate(biAppSqlSessionFactory);
//    }
//
//    @Primary
//    @Bean
//    public DataSourceTransactionManager biAppTransactionManager(@Qualifier("shardingDataSource")DataSource shardingDataSource) {
//        return new DataSourceTransactionManager(shardingDataSource);
//    }
//
//}
